package cz.ctu.android.DriveCommunicator.activities;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import cz.ctu.android.DriveCommunicator.R;
import cz.ctu.android.DriveCommunicator.utils.DriverConstants;
import cz.ctu.android.DriveCommunicator.utils.HandlerMessage;
import cz.ctu.android.DriveCommunicator.utils.ImageMapper;
import cz.ctu.android.DriveCommunicator.utils.DriverConstants.Users;
import cz.ctu.android.DriveCommunicator.utils.Zprava;

public class SuccessActivity extends Activity{
	private ImageView confirmButton;
	private Bundle bundle;
	private Handler handler;
	private Timer t;
	
	private OnClickListener confirmClickListener=new OnClickListener(){

		public void onClick(View v) {
			t.cancel();
			Intent intent=new Intent(SuccessActivity.this,MainActivity.class);
	        startActivity(intent);       
		}
    	
    };
    
    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.success);
	    bundle = this.getIntent().getExtras();
		int id=bundle.getInt("idToLoad");
		String message = bundle.getString("message");
		
		confirmButton = (ImageView)findViewById(R.id.receiverImage);
		confirmButton.setImageResource(ImageMapper.getImage(id));
		confirmButton.setOnClickListener(confirmClickListener);
		
		ImageView butt =  (ImageView)findViewById(R.id.imageView1);
		butt.setOnClickListener(confirmClickListener);
		
		TextView text = (TextView) findViewById(R.id.receiverName);
		text.setText(DriverConstants.Users.getUserByID(id).getJmeno()+" "+DriverConstants.Users.getUserByID(id).getPrijmeni());
		
		HandlerMessage hand = new HandlerMessage();
		Zprava z = new Zprava(id, message);
		try {
			hand.addMessage(z);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		t = new Timer();
		handler = new Handler(); 
	        t.schedule(new TimerTask() { 
	                public void run() { 
	                        handler.post(new Runnable() { 
	                                public void run() { 
	                                	Intent intent=new Intent(SuccessActivity.this,MainActivity.class);
	                        	        startActivity(intent); 
	                                } 
	                        }); 
	                } 
	        }, 10000);
	}
    
   
}
